package com.example.java;

import org.apache.ibatis.session.TransactionIsolationLevel;

import java.io.Externalizable;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.nio.BufferOverflowException;
import java.util.Hashtable;

public class Test {

	float f1;
	double d1;
	boolean b1;
	public static int Fib(int n) {
//		this
//		super
		/*
		 * if(n==1){ return 0; }else
		 */if (n < 3) {
			return 1;
		}
		return Fib(n - 1) + Fib(n - 2);
	}

//	TransactionIsolationLevel.
	public static int retTry() {

		try {
			return 1;
		} catch (Exception e) {
		} finally {
			System.out.println("ppoo");
			return 2;//2发替换1
		}
	}
	public static int retTry1() {
		int ret=1;

		try {
			ret=2;//ttt
			return ret;
		} catch (Exception e) {
			return 0;
		} finally {
			System.out.println("ppoo");
			ret=3;
			//return ret;//不return 就是2
		}
	}
	public static StringBuffer retTry2() {
		StringBuffer sb = new StringBuffer("hello ");
		try {
			return sb;
		} catch (Exception e) {
			return null;
		} finally {
			sb.append(" wh");
			System.out.println("---sbpppp");
		}
	}
	
	
	public static void retFinaly() {
		int g = 5 / 0;
		try {
			System.out.println("---try");
		} catch (Exception e) {
			System.out.println("---catch");
		} finally {
			System.out.println("---finally");
		}
	}

	public static void retFinaly2() {
		try {
			System.out.println("---try");
			System.exit(0);
		} catch (Exception e) {
			System.out.println("---catch");
		} finally {
			System.out.println("---finally");
		}
	}
	public static synchronized void main(String[] args) {

		System.out.println(Fib(1));
		System.out.println(Fib(2));
		System.out.println(Fib(3));
		System.out.println(Fib(4));
		System.out.println(Fib(5));
		System.out.println(Fib(6));
		System.out.println(54 % 10);
		System.out.println("---------------");
		
		// 测试
        System.out.println(countNumOf1(13));
        System.out.println(countNumOf1(23));
        System.out.println(countNumOf1(33));
        System.out.println(countNumOf1(93));
        System.out.println(countNumOf1(123));
        System.out.println(countNumOf1(534));
        System.out.println(count(534));
        System.out.println(myCount(13));

        
		try {

		} catch (Exception e) {

		}

		try {

		} finally {

		}
			
		
		Object ooosb;
		//StaticTA sta= new StaticTA(); //抽象类不能实例化
		AbstractTA.sssB();
		Test test1=new  Test();
		System.out.println("---------------");

//		for (int i = 0; i < 10; i++) {
//			for (int j = 0; j < 10; j++) {
//				System.out.println(i+"-"+j);
//				if(j==5){
//					return; //跳出2层循环
//					//break;
//				}
//			}
//		}
		
		//assert 1+1==2;
		//new Test().kkkk2();
		System.out.println("==================");

		float ff1=4.99990f;
		float ff2=(float) 0.99990;
//		boolean fff=null;
		System.out.println(test1.f1);
		System.out.println(test1.d1);
		System.out.println(test1.b1);
		
		int ertt=retTry();
		System.out.println(ertt);
		int ertt1=retTry1();
		System.out.println(ertt1);
		StringBuffer ertt2=retTry2();
		System.out.println(ertt2.toString());
		//retFinaly(); //finally不一定执行
		retFinaly2();
		
		//运行时异常不用强制捕获
//		IndexOutOfBoundsException
//		NullPointerException
//		ClassCastException
//		ArrayStoreException //数组存储异常
//		BufferOverflowException 缓冲区溢出
//		ArithmeticException
		
//		OutOfMemoryError
//		ThreadDeath
		
//		checkedException  IO/SQL 编译阶段,强制捕获
//		先捕获子类再捕获基类
		ObjectInput on;
		ObjectInputStream ons;
//		Hashtable<K, V> ht;
		Externalizable ela;
	}

	public void kkkk2(){
		final int ss=0;
		int ss2;//必须初始化
		System.out.println(ss);
//		System.out.println(ss2);
	}
	protected void finalize() throws Throwable{
		super.finalize();
		System.out.println("-finalize-");
	}
	public static int myCount(int n){
		if(n<1){
			return 0;
		}
		int count=0;//技术
		int base=1;//权重
		int round=n;//轮数
		while(round>0){
			int weight = round % 10;// 当前位数当前值
			round /= 10; //当前位数轮数
			count += round * base;
			if(weight ==1){
				count +=  (n%base +1);
			}else if ( weight>1){
				count +=  base;
			}
			
			
			base *= 10;
		}
		
		return count;
		
	}
	
	
	public static int count(int n){
	    if(n<1)
	        return 0;
	    int count = 0;
	    int base = 1;
	    int round = n;
	    while(round>0){
	        int weight = round%10;
	        round/=10;
	        count += round*base;
	        if(weight==1)
	            count+=(n%base)+1;
	        else if(weight>1)
	            count+=base;
	        base*=10;
	    }
	    return count;
	}
	
	
	// 思路：分别计算“1”在每个位上面出现的次数，叠加起来
	public static int countNumOf1(int n) {
		if (n <= 0) {
			return 0;
		}
		int count = 0;
		int factor = 1;
		while (n / factor != 0) {
			int lowerNum = n - n / factor * factor;
			int currentNum = (n / factor) % 10;
			int highNum = n / (factor * 10);

			if (currentNum == 0) {
				// 如果为0,出现1的次数由高位决定
				count += highNum * factor;
			} else if (currentNum == 1) {
				// 如果为1,出现1的次数由高位和低位决定
				count += highNum * factor + lowerNum + 1;
			} else {
				// 如果大于1,出现1的次数由高位决定
				count += (highNum + 1) * factor;
			}
			factor *= 10;
		}
		return count;
	}

}
